package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.constants.SystemConstants;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.RoleSaveDto;
import com.feng.domain.dto.RoleUpdateDto;
import com.feng.domain.entity.Menu;
import com.feng.domain.entity.Role;
import com.feng.domain.entity.RoleMenu;
import com.feng.domain.entity.User;
import com.feng.domain.vo.PageVo;
import com.feng.domain.vo.RoleGetVo;
import com.feng.mapper.RoleMapper;
import com.feng.service.RoleMenuService;
import com.feng.service.RoleService;
import com.feng.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2022-10-15 11:32:22
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<String> selectRoleKeyByUserID(Long userId) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if(userId==1L){
          List<String> list=new ArrayList<>();
          list.add("admin");
          return list;
        }
        //否则查询用户所具有的角色信息
        return roleMapper.selectRoleKeyByUserID(userId);
    }

    @Override
    public ResponseResult selectRolePage(Role role, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //目前没有根据id查询
//        lambdaQueryWrapper.eq(Objects.nonNull(role.getId()),Role::getId,role.getId());
        lambdaQueryWrapper.like(StringUtils.hasText(role.getRoleName()),Role::getRoleName,role.getRoleName());
        lambdaQueryWrapper.eq(StringUtils.hasText(role.getStatus()),Role::getStatus,role.getStatus());
        lambdaQueryWrapper.orderByAsc(Role::getRoleSort);

        Page<Role> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,lambdaQueryWrapper);

        //转换成VO
        List<Role> roles = page.getRecords();

        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(roles);
        return ResponseResult.okResult(pageVo);
    }


    @Autowired
    private RoleMenuService roleMenuService;
    @Override
    public ResponseResult saveRole(RoleSaveDto roleSavevo) {
        Role role = BeanCopyUtils.copyBean(roleSavevo, Role.class);
        this.save(role);
        if(roleSavevo.getMenuIds()!=null&&roleSavevo.getMenuIds().size()>0){
            List<RoleMenu> collect = roleSavevo.getMenuIds().stream().map(e -> new RoleMenu(role.getId(), e)).
                    collect(Collectors.toList());
            roleMenuService.saveBatch(collect);
        }
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult getRoleById(Long id) {
        Role role = this.getById(id);
        RoleGetVo roleGetVo = BeanCopyUtils.copyBean(role, RoleGetVo.class);
        return ResponseResult.okResult(roleGetVo);
    }
    @Transactional
    @Override
    public ResponseResult updateByDetails(RoleUpdateDto roleUpdateDto) {
        Role role = BeanCopyUtils.copyBean(roleUpdateDto, Role.class);
        this.updateById(role);
        roleMenuService.deleteRoleMenuByRoleId(roleUpdateDto.getId());
        List<Long> menuIds = roleUpdateDto.getMenuIds();
        for (Long menuId : menuIds) {
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleUpdateDto.getId());
            roleMenuService.save(roleMenu);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listRole() {
        LambdaQueryWrapper<Role> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.STATUS_NORMAL);
        List<Role> list = this.list(queryWrapper);
        return ResponseResult.okResult(list);
    }




}

